package OrderBaseService

import (
	"context"
	"gdshop-font-api-go/app/response/OrderResp"
	"gdshop-font-api-go/library/tools"
	toolsDb "gdshop-font-api-go/library/tools/db"
	"github.com/gogf/gf/frame/g"
)

func GetNextStatusText() g.Map {
	return g.Map{
		"0": "关闭订单",
		"1": "付款",
		"4": "确认收货",
		"5": "评价",
	}
}
func GetStatusText() g.Map {
	return g.Map{
		"0":  "交易关闭",
		"1":  "等待买家付款",
		"2":  "买家已付款",
		"3":  "卖家已备货",
		"4":  "卖家已发货",
		"5":  "交易成功",
		"99": "交易成功",
	}
}
func GetRefundStatusText() g.Map {
	return g.Map{
		"0":  "退换",
		"1":  "退换中",
		"2":  "拒绝",
		"3":  "同意",
		"99": "退款完成",
	}
}

func OrderIsShowPrimary(status int) bool {
	switch status {
	case 5:
		return true
	case 4:
		return true
	case 1:
		return true
	default:
		return false
	}
}

func GetOrderTipsList(orderId int) []*OrderResp.OrderTips {
	if orderId%2 == 1 {
		return []*OrderResp.OrderTips{
			&OrderResp.OrderTips{
				Title:  "15天价保",
				Remark: "1.26-2.9为保价服务期",
				Status: "已结束",
			},
		}
	} else {
		return []*OrderResp.OrderTips{}
	}

}

func GetOrderSubByOrderId(ctx context.Context, orderId int) []*OrderResp.SubOrder {
	var subOrderList []*OrderResp.SubOrder
	err := toolsDb.GetUnSafaTableAddDeleteWhere(ctx, "order_sub").Order("id DESC").Where(
		"order_id=?",
		orderId,
	).Structs(&subOrderList)
	if err != nil {
		g.Log().Async().Error("GetOrderSubByOrderId错误 ", orderId, err.Error())
		return nil
	}

	for _, item := range subOrderList {
		item.ThumbsArr = tools.GetGoodsThumbs(item.GoodsThumbs)
		if len(item.ThumbsArr) > 0 {
			item.CoverPhoto = item.ThumbsArr[0]
		} else {
			// 设置默认图片
		}
	}

	return subOrderList
}

func OrderIsExpress(ctx context.Context, orderId int) bool {
	count, err := toolsDb.GetUnSafaTable(ctx, "order_express").Distinct().As("oe").
		LeftJoin("order_sub os", "oe.order_sub_id = os.id").Where(
		"oe.delete_at < 1 AND (os.order_id = ? OR oe.order_id = ?)", orderId,
		orderId,
	).Fields(
		"1").Count()
	if err != nil {
		return false
	}

	return count > 0
}
